package com.graduation.rescuematerial.controller.applet;

import com.graduation.rescuematerial.service.MaterialService;
import com.graduation.rescuematerial.service.UserService;
import com.graduation.rescuematerial.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/4/24 14:17
 * @Version:
 * @Description:TODO
 */
@RestController
@RequestMapping("/applet/material")
@Api(tags = "Applet——物资模块")
public class AppletMaterialController {

    @Autowired
    private MaterialService materialService;
    @Autowired
    private UserService userService;


    @ApiOperation("查询可申请的物资列表(库存大于零的物资)")
    @ResponseBody
    @GetMapping("/applicable")
    public RespBean getApplicableMaterial()
    {
        //待修改（按照数量最多，距离最近排序）
        return materialService.getApplicableMaterial();
    }


    @ApiOperation("/获取本部门物资数字统计")
    @ResponseBody
    @PostMapping("/statistics")
    public RespBean getStatistics(@RequestBody String ticket, HttpServletRequest request, HttpServletResponse response){
        //获取cookie
//        String ticket= CookieUtil.getCookieValue(request,"userTicket");
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                String companyName=userVo.getCompanyName();
                return materialService.getStatistics(companyName);
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("根据物资名称模糊查询本组织下的物资")
    @ResponseBody
    @PostMapping("/fuzzy/query")
    public RespBean fuzzyQuery(@RequestBody FuzzyQueyMaterialVo fuzzyQueyMaterialVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=fuzzyQueyMaterialVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                String companyName=userVo.getCompanyName();
                return materialService.fuzzyQuery(companyName,fuzzyQueyMaterialVo.getKey());
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("获取本部门下所有物资")
    @ResponseBody
    @PostMapping("/all")
    public RespBean getAll(@RequestBody String ticket,HttpServletRequest request, HttpServletResponse response){
        if(ticket!=null && !ticket.isEmpty()){
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                String companyName=userVo.getCompanyName();
                return materialService.getAll(companyName);
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("获取指定目录下的物资")
    @ResponseBody
    @PostMapping("/get/catalog")
    public RespBean getAllByCatalog(@RequestBody MaterialCatalogVo materialCatalogVo, HttpServletRequest request, HttpServletResponse response){
        String ticket=materialCatalogVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                String companyName=userVo.getCompanyName();
                return materialService.getAllByCatalog(companyName,materialCatalogVo.getCatalogId());
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);

    }

    @ApiOperation("获取指定仓库下的可申请物资(传入catalog则可获取指定目录，keyword和catalog可联合查询)")
    @ResponseBody
    @PostMapping("/get/warehouseName")
    public RespBean getApplicableMaterialByWareHouseName(@RequestBody ApplicableByWareHouseNameVo applicable,HttpServletRequest request, HttpServletResponse response){
        String ticket=applicable.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                return materialService.getApplicableMaterialByWareHouseName(applicable);
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    //修改物资（物资状态是否紧急，物资数量）
    @ApiOperation("修改本部门物资，是否紧急以及物资数量")
    @ResponseBody
    @PostMapping("/update")
    public RespBean updateMaterial(@RequestBody UpdateMaterialVo materialVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=materialVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                return materialService.updateMaterial(materialVo,userVo.getCompanyName());
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    //添加物资
    @ApiOperation("组织和仓库直接添加物资:直接调用sysMaterial_add,该模块有物资下拉框")
    @ResponseBody
    @PostMapping("/add")
    public RespBean addMaterial(){
        return null;
    }
}

